# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base  
  before_filter :check_authentication, :except =>:signin
  
  def index
    #raise params.inspect
  end
  
  
  def check_authentication
    unless session[:user]
      flash[:notice]="Please log in"
      session[:intended_action] = action_name
      session[:intended_controller] = controller_name
      redirect_to  :controller => "login", :action => "signin"
    end
  end
  
  def signin
    if request.post?
      session[:user] = User.authenticate(params[:username], params[:password]).id
      redirect_to :action => session[:intended_action], :controller => session[:intended_controller]
    end
  rescue RuntimeError 
    redirect_to "/"
  end   
  
  def signout
    session[:user] = nil
    redirect_to "/"
  end
  
  private
  def redirect_to_index(msg = nil)
    flash[:notice ] = msg if msg
    redirect_to(:action =>'index')
  end
  
end